package com.els.util;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.lucene.search.TermQuery;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.MatchQueryBuilder.Operator;
import org.elasticsearch.index.query.QueryBuilders;

public class QueryHelper {
	public static BoolQueryBuilder createQuery(String qString,String[] columns,float[] boosts){
		BoolQueryBuilder mainQuery=QueryBuilders.boolQuery();
		mainQuery.minimumNumberShouldMatch(1);
		List<String> terms=splitQueryString(qString);
		for (int i=0;i<columns.length;i++) {
			String column=columns[i];	//列明
			BoolQueryBuilder columnQuery=QueryBuilders.boolQuery();
			for (String term : terms) {
				BoolQueryBuilder termQuery=QueryBuilders.boolQuery();
				termQuery.must(QueryBuilders.matchQuery(column,term).operator(Operator.AND));			//match匹配
				termQuery.should(QueryBuilders.matchPhraseQuery(column, term).slop(50));	//语法匹配
				columnQuery.must(termQuery);
			}
			columnQuery.boost(boosts[i]);//权重
			mainQuery.should(columnQuery);		
		}
		return mainQuery;
	}
	public static BoolQueryBuilder createQueryNumber(String qString,String[] columns,float[] boosts){
		BoolQueryBuilder mainQuery=QueryBuilders.boolQuery();
		mainQuery.minimumNumberShouldMatch(1);
		List<String> terms=splitQueryString(qString);
		for (String term : terms) {
			BoolQueryBuilder termQuery=QueryBuilders.boolQuery();
			for (int i=0;i<columns.length;i++) {
				String column=columns[i];	//列明
				if(column.equals("title")){
					List<String> numbers=getNumber(term);
					String termNoNumber=term;
					BoolQueryBuilder titleQuery=QueryBuilders.boolQuery();
					for (String number : numbers) {
						termNoNumber=term.replace(number, " ");
						titleQuery.must(QueryBuilders.wildcardQuery(column, "*"+number+"*"));
						
					}
					if(!termNoNumber.replace(" ", "").equals("")){
						titleQuery.must(QueryBuilders.matchPhraseQuery(column, termNoNumber).slop(5));	//语法匹配
					}
					termQuery.should(titleQuery.boost(boosts[i]));
					continue;
				}
				if(!term.replace(" ", "").equals("")){
					termQuery.should(QueryBuilders.matchPhraseQuery(column, term).slop(5).boost(boosts[i]));	//语法匹配
				}
			}
			mainQuery.must(termQuery);		
		}
		return mainQuery;
	}
	public static BoolQueryBuilder createQueryByfield(String qString,String[] columns,float[] boosts){
		BoolQueryBuilder mainQuery=QueryBuilders.boolQuery();
		mainQuery.minimumNumberShouldMatch(1);
		List<String> terms=splitQueryString(qString);
			for (String term : terms) {
				BoolQueryBuilder termQuery=QueryBuilders.boolQuery();

				if(!term.replace(" ", "").equals("")){
					for (int i=0;i<columns.length;i++) {
						String column=columns[i];	//列明
						termQuery.should(QueryBuilders.matchPhraseQuery(column, term).slop(5).boost(boosts[i]));	//语法匹配
					}
					
				}
			mainQuery.must(termQuery);		
		}
		return mainQuery;
	}
	public static BoolQueryBuilder createShouldQueryByfield(String qString,String[] columns,float[] boosts){
		BoolQueryBuilder mainQuery=QueryBuilders.boolQuery();
		mainQuery.minimumNumberShouldMatch(1);
		List<String> terms=splitQueryString(qString);
			for (String term : terms) {
				BoolQueryBuilder termQuery=QueryBuilders.boolQuery();

				if(!term.replace(" ", "").equals("")){
					for (int i=0;i<columns.length;i++) {
						String column=columns[i];	//列明
						termQuery.should(QueryBuilders.matchPhraseQuery(column, term).slop(5).boost(boosts[i]));	//语法匹配
					}
					
				}
			mainQuery.should(termQuery);		
		}
		return mainQuery;
	}
	public static List<String> splitQueryString(String qString) {
		qString=removePunc(qString);
		String[] tmpArr=qString.split(" ");
		List<String> results=new ArrayList<String>();
		for (String tmp : tmpArr) {
			if(tmp!=null&&!tmp.equals(""))
			{
				results.add(tmp);
			}
		}
		return results;
	}
	public static List<String> getNumber(String qString) {
		String regEx="[^0-9]";   
		Pattern p = Pattern.compile(regEx);   
		Matcher m = p.matcher(qString);   
		String[] tmpArr=m.replaceAll(" ").split(" ");
		List<String> numbers=new ArrayList<String>();
		for (String tmp : tmpArr) {
			if(tmp!=null&&!tmp.equals(""))
			{
				numbers.add(tmp);
			}
		}
		return numbers;
	}
	public static List<String> getIdCard(String qString) {
		String regEx="[^0-9xX]";   
		Pattern p = Pattern.compile(regEx);   
		Matcher m = p.matcher(qString);   
		String[] tmpArr=m.replaceAll(" ").split(" ");
		List<String> numbers=new ArrayList<String>();
		for (String tmp : tmpArr) {
			if(tmp!=null&&!tmp.equals(""))
			{
				numbers.add(tmp);
			}
		}
		return numbers;
	}
	public static String removePunc(String term) {
		term=term.replace("。", " ");
		term=term.replace("，", " ");
		term=term.replace("、", " ");
		term=term.replace("！", " ");
		term=term.replace("？", " ");
		term=term.replace(",", " ");
		term=term.replace(".", " ");
		term=term.replace("!", " ");
		term=term.replace("?", " ");
		return term;
	}
	
	
}
